# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, Form
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from typing import List
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.entity.vo.import_vo import ImportModel
from module_admin.service.import_service import ImportService
from module_admin.service.login_service import LoginService
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.annotation.log_annotation import Log
from utils.response_util import ResponseUtil
from utils.common_util import bytes2file_response

from module_stock.entity.vo.dc_member_vo import DcMemberPageModel, DcMemberModel
from module_stock.service.dc_member_service import DcMemberService

dcMemberController = APIRouter(prefix='/stock/dc_member', dependencies=[Depends(LoginService.get_current_user)])


@dcMemberController.get('/list', dependencies=[Depends(CheckUserInterfaceAuth('stock:dc_member:list'))])
async def get_dc_member_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: DcMemberPageModel = Depends( DcMemberPageModel.as_query),
        data_scope_sql: str = Depends(GetDataScope('DcMember'))
):
    dc_member_result = await DcMemberService.get_dc_member_list(query_db, page_query, data_scope_sql)

    return ResponseUtil.success(model_content=dc_member_result)

@dcMemberController.get('/getById/{dcMemberId}', dependencies=[Depends(CheckUserInterfaceAuth('stock:dc_member:list'))])
async def get_dc_member_by_id(
        request: Request,
        dcMemberId: int,
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('DcMember'))
):
    dc_member = await DcMemberService.get_dc_member_by_id(query_db, dcMemberId)
    return ResponseUtil.success(data=dc_member)


@dcMemberController.post('/add', dependencies=[Depends(CheckUserInterfaceAuth('stock:dc_member:add'))])
@Log(title='dc_member', business_type=BusinessType.INSERT)
async def add_dc_member (
    request: Request,
    add_model: DcMemberModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):

    add_model.create_by = current_user.user.user_id
    add_model.dept_id = current_user.user.dept_id
    add_dict_type_result = await DcMemberService.add_dc_member(query_db, add_model)
    return ResponseUtil.success(data=add_dict_type_result)

@dcMemberController.put('/update', dependencies=[Depends(CheckUserInterfaceAuth('stock:dc_member:edit'))])
@Log(title='dc_member', business_type=BusinessType.UPDATE)
async def update_dc_member(
    request: Request,
    edit_model: DcMemberModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_dict_type_result = await DcMemberService.update_dc_member(query_db, edit_model)
    return ResponseUtil.success(data=add_dict_type_result)


@dcMemberController.delete('/delete/{dcMemberIds}', dependencies=[Depends(CheckUserInterfaceAuth('stock:dc_member:del'))])
@Log(title='dc_member', business_type=BusinessType.DELETE)
async def del_dc_member(
    request: Request,
    dcMemberIds: str,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    ids = dcMemberIds.split(',')
    del_result = await DcMemberService.del_dc_member(query_db, ids)
    return ResponseUtil.success(data=del_result)

@dcMemberController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('stock:dc_member:export'))])
@Log(title='dc_member', business_type=BusinessType.EXPORT)
async def export_dc_member(
    request: Request,
    dc_member_form: DcMemberPageModel = Form(),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('DcMember')),
):
    # 获取全量数据
    export_result = await DcMemberService.export_dc_member_list(
        query_db, dc_member_form, data_scope_sql
    )
    return ResponseUtil.streaming(data=bytes2file_response(export_result))

@dcMemberController.post('/import', dependencies=[Depends(CheckUserInterfaceAuth('stock:dc_member:import'))])
async def import_dc_member(request: Request,
                      import_model: ImportModel,
                      query_db: AsyncSession = Depends(get_db),
                      current_user: CurrentUserModel = Depends(LoginService.get_current_user)
    ):
    """
    导入数据
    """
    await ImportService.import_data(query_db, import_model, current_user)
    return ResponseUtil.success()